 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package putime.model;

import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import putime.db.DBModel;
import putime.newspaperagent.supplier.Supplier;

/**
 *
 * @author Khai
 */
public class ModelSupplier extends DBModel {

    Vector<Supplier> vSp;
   
    public Vector<Supplier> initComboxSupplier() {
        vSp = new Vector<>();
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers");
            rs = prst.executeQuery();
            while (rs.next()) {
                Supplier sp = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
                vSp.add(sp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnect();
        return vSp;
    }

    public Supplier ComboChange(int chon) {
        openConnect();
        Supplier s = null;
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where SupID = ?");
            prst.setInt(1, chon);
            rs = prst.executeQuery();
            rs.next();
            s = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnect();
        return s;

    }

    public boolean deleteSupplier(int chon) {
        openConnect();
        try {
            prst = conn.prepareStatement("delete from tbl_suppliers where SupID = ?");
            prst.setInt(1, chon);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }
    }

    public boolean editSupplier(int chon, String name, String address, String telephone) {
        openConnect();
        try {
            prst = conn.prepareStatement("update tbl_suppliers set SupName = ?, Address = ?, Tel = ? where supID = ?");
            prst.setString(1, name);
            prst.setString(2, address);
            prst.setString(3, telephone);
            prst.setInt(4, chon);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }
    }

    public boolean addSupplier(String name, String address, String telephone) {
        openConnect();
        try {
            prst = conn.prepareStatement("insert tbl_suppliers(SupName,Address,Tel) values (?,?,?)");
            prst.setString(1, name);
            prst.setString(2, address);
            prst.setString(3, telephone);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }
    }

    public Vector<Supplier> findID(int chon) {
         Vector<Supplier> vSup = new Vector<>();
        Supplier sup;
        openConnect();       
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where SupID = ?");
            prst.setInt(1, chon);
            rs = prst.executeQuery();
            while (rs.next()) {
                 sup = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
                vSup.add(sup);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }
         return vSup;
    }

    public Vector<Supplier> findName(String chon) {
        Vector<Supplier> vSup = new Vector<>();
        Supplier sup;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where SupName LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                sup = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
                vSup.add(sup);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vSup;
    }
    
     public Vector<Supplier> findAddress(String chon) {
        Vector<Supplier> vSup = new Vector<>();
        Supplier sup;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where Address LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                sup = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
                vSup.add(sup);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vSup;
    }
     
      public Vector<Supplier> findTel(String chon) {
        Vector<Supplier> vSup = new Vector<>();
        Supplier sup;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where Tel LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                sup = new Supplier(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
                vSup.add(sup);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vSup;
    }
      
       public boolean checkExist(String name){
          openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_suppliers where SupName = ?");
            prst.setString(1, name);
            rs = prst.executeQuery();
            while(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelLine.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            closeConnect();
        }
        return false;
          
      }
}
